package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.StockIn;
import com.ericom.service.GenericManager;

public class StockInAction extends BaseAction {
	private GenericManager<StockIn, Long> stockInManager;
	private List stockIns;
	private StockIn stockIn;
	private Long id;

	public StockIn getStockIn() {
		return stockIn;
	}

	public void setStockIn(StockIn stockIn) {
		this.stockIn = stockIn;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setStockInManager(GenericManager<StockIn, Long> stockInManager) {
		this.stockInManager = stockInManager;
	}

	public String list() {
		stockIns = stockInManager.getAll();
		return SUCCESS;
	}

	public List getStockIns() {
		return stockIns;
	}

	public String delete() {
		stockInManager.remove(stockIn.getId());
		this.saveMessage(getText("stockIn.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			stockIn = stockInManager.get(id);
		} else {
			stockIn = new StockIn();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (stockIn.getId() == null);
		stockIn = stockInManager.save(stockIn);

		String key = (isNew) ? "stockIn.added" : "stockIn.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
